// pages/leave/leave.js
var dateTimePicker = require('../../utils/dateTimePicker.js') //引入外部的js工具类
const {
    config
} = require('../../utils/config.js')
const {
    imgConfig
} = require('../../utils/config.js')
const mydate = new Date();
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: '12:00',
        dateTimeArray: null,
        dateTime: null,
        dateTimeArray1: null,
        dateTime1: null,
        startYear: 2000,
        endYear: 2050,
        imgUrl: `${imgConfig}`,
        statusBarHeight: app.globalData.statusBarHeight,
        checks: 0,
        date: "",
        remark: "",
        id: '',
        reason: "",
        beginDate: '',
        beginTime: '',
        endDate: '',
        endTime: '',
        leaveHours:0
    },
    back: function () {
        wx.navigateBack({});
    },
    updateLeave(id){
        var that = this
        wx.request({
            url: `${config}/wechat/wxleave/getLeave`,
            data: {
                id: id
            },
            success(resp) {
                console.log(resp)
                var leave = resp.data.leave
                that.setData({
                    applyDate: leave.applyDate,
                    reason: leave.leaveReason,
                    remark: leave.remark,
                    id: leave.id,
                    status:leave.status
                })
            }
        })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {

        var that = this
        //判断是否为修改申请
        if (e.id != undefined) {
            that.updateLeave(e.id)
        }

        let isIphoneX = app.globalData.isIphoneX;
        this.setData({
            isIphoneX: isIphoneX
        });
        // 获取完整的年月日 时分秒，以及默认显示的数组
        var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        // 精确到分的处理，将数组的秒去掉
        var lastArray = obj.dateTimeArray.pop();
        var lastTime = obj.dateTime.pop();
        var lastArray1 = obj1.dateTimeArray.pop();
        var lastTime1 = obj1.dateTime.pop();
        this.setData({
            dateTime: obj.dateTime,
            dateTimeArray: obj.dateTimeArray,
            dateTimeArray1: obj1.dateTimeArray,
            dateTime1: obj1.dateTime
        });
        var arr = obj1.dateTime
        var dateArr = obj1.dateTimeArray
        this.setData({
            beginDate: dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]],
            beginTime: dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]],
            endDate: dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]],
            endTime: dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]]

        })
    },
    /**
     * 选择申请日期
     */
    bindDateChange: function (e) {
    },
    //input数据绑定
    bindRemark(e) {
        let value = e.detail.value
        this.setData({
            remark: value
        })
    },
    /**
     * 请假原因
     */
    bindReason(e) {
        let value = e.detail.value
        this.setData({
            reason: value
        })
    },

    /**
     * 开始时间
     * @param {*} e 
     */
    changeDateTime1(e) {
        let leaveHours = this.getleaveHours()
          var myDate = new Date;
          var year = myDate.getFullYear(); //获取当前年
          var mon = myDate.getMonth() + 1; //获取当前月
          var date = myDate.getDate(); //获取当前日
          var now = year + "-" + mon + "-" + date ;
        this.setData({
            dateTime1: e.detail.value,
            leaveHours:leaveHours,
            applyDate: now
            
        });
    },

    changeDateTimeColumn1(e) {
      console.log(e);
        var arr = this.data.dateTime1,
        dateArr = this.data.dateTimeArray1;

        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

        this.setData({
            dateTimeArray1: dateArr,
            dateTime1: arr,
            beginDate: dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]],
            beginTime: dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]]
        });
        console.log(this.data.beginDate)
    },
    /**
     * 结束时间
     * @param {*} e 
     */
    changeDateTime(e) {
      let leaveHours = this.getleaveHours()
        var myDate = new Date;
        var year = myDate.getFullYear(); //获取当前年
        var mon = myDate.getMonth() + 1; //获取当前月
        var date = myDate.getDate(); //获取当前日
        var now = year + "-" + mon + "-" + date ;
        this.setData({
            dateTime: e.detail.value,
            leaveHours:leaveHours,
            applyDate: now
        });
    },

    changeDateTimeColumn(e) {
        var arr = this.data.dateTime,dateArr = this.data.dateTimeArray;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        this.setData({
            dateTimeArray: dateArr,
            dateTime: arr,
            endDate: dateArr[0][arr[0]] + "-" + dateArr[1][arr[1]] + "-" + dateArr[2][arr[2]],
            endTime: dateArr[3][arr[3]] + ":" + dateArr[4][arr[4]],
        });
    },
    sendLeave: function () {
        var that = this;
        //拼接时间,将数组格式的时间转换成yyyy-MM-dd HH:mm
        //开始时间
        var year1 = "20" + this.data.dateTime1[0]
        var month1 = ""
        var day1 = ""
        if (this.data.dateTime1[1] < 10) {
            month1 = "0" + this.data.dateTime1[1]
        } else {
            month1 = this.data.dateTime1[1]
        }
        if (this.data.dateTime1[2] < 10) {
            day1 = "0" + this.data.dateTime1[2]
        } else {
            day1 = this.data.dateTime1[2]
        }
        var hour1 = ''
        var minute1 = ''
        if (this.data.dateTime1[3] < 10) {
            hour1 = "0" + this.data.dateTime1[3]
        } else {
            hour1 = this.data.dateTime1[3]
        }

        if (this.data.dateTime1[4] < 10) {
            minute1 = "0" + this.data.dateTime1[4]
        } else {
            minute1 = this.data.dateTime1[4]
        }
        //结束时间
        var year = "20" + this.data.dateTime[0]
        var month = ""
        var day = ""
        if (this.data.dateTime[1] < 10) {
            month = "0" + this.data.dateTime[1]
        } else {
            month = this.data.dateTime[1]
        }

        if (this.data.dateTime[2] < 10) {
            day = "0" + this.data.dateTime[2]
        } else {
            day = this.data.dateTime[2]
        }
        var hour = ''
        var minute = ''
        if (this.data.dateTime[3] < 10) {
            hour = "0" + this.data.dateTime[3]
        } else {
            hour = this.data.dateTime[3]
        }

        if (this.data.dateTime[4] < 10) {
            minute = "0" + this.data.dateTime[4]
        } else {
            minute = this.data.dateTime[4]
        }
        console.log(this.data.beginDate);
        var beginDate = this.data.beginDate
        var beginTime = hour1 + ":" + minute1 + ":" + "00" //后台属性为Time格式 拼接匹配格式HH:mm:ss
        var beginDateTime = beginDate + beginTime
        var endDate = this.data.endDate
        var endTime = hour + ":" + minute + ":" + "00" //后台属性为Time格式 拼接匹配格式HH:mm:ss
        var endDateTime = endDate + endTime
        var applyDate = that.data.applyDate
        var leaveReason = this.data.reason
        var remark = this.data.remark
        console.log(applyDate)
        if (applyDate != "" && applyDate != null && leaveReason != null && leaveReason != ""&&beginDateTime < endDateTime) {
            wx.request({
                url: `${config}/wechat/wxleave/${this.data.id !="" ?'updateLeave':'addleave'}`,
                method: "post",
                header: {
                    "Content-Type": "application/json"
                },
                data: {
                    beginDate: beginDate,
                    beginTime: beginTime,
                    endTime: endTime,
                    endDate: endDate,
                    applyDate: applyDate,
                    leaveReason: leaveReason,
                    remark: remark,
                    stuId: app.globalData.userInfo.id,
                    id:that.data.id,
                    status:that.data.status
                },
                success(res) {
                    // 返回值
                    console.log(res.data)
                    if (res.data.code == 0) {
                        wx.showToast({
                            title: '操作成功',
                            icon: 'success',
                            duration: 1500
                        })
                        setTimeout(function () {
                            wx.switchTab({
                                url: '/pages/index/index',
                            })
                        }, 1500)
                    } else {
                        wx.showToast({
                            title: '操作失败',
                            icon: 'none',
                            duration: 1500
                        })
                    }
                }

            })
        } else if (beginDateTime >= endDateTime) {
            wx.showToast({
                title: '结束时间不能早于开始时间!',
                icon: 'none',
                duration: 1500
            })
        } else if (applyDate == null || applyDate == "") {
            wx.showToast({
                title: '申请日期不能为空!',
                icon: 'none',
                duration: 1500
            })

        } else if (leaveReason == null || leaveReason == "") {
            wx.showToast({
                title: '申请原因不能为空!',
                icon: 'none',
                duration: 1500
            })
        }


    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    getleaveHours() {
      let leaveHours = 0
      let newBeginDate =this.data.beginDate+' '+ this.data.beginTime+':'+'00'
      let newEndDate =this.data.endDate+' '+ this.data.endTime+':'+'00'
      var diyDateBegin = new Date(newBeginDate)
      var diyDateEnd = new Date(newEndDate)
        if(diyDateBegin.getTime()<diyDateEnd.getTime()){
          let time = diyDateEnd.getTime() - diyDateBegin.getTime()
          const days = Math.floor(time / (1000 * 60 * 60 * 24));
          const hours = Math.floor((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
          const minutes = Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
          if (days> 0) {
            leaveHours= (hours+(days*8)+'.'+minutes)
            return leaveHours;
          }else{
            leaveHours = (hours+'.'+minutes)
            console.log(leaveHours);
            return leaveHours;
          }
        }else{
          console.log('2222');
        }
    }

})